import 'package:easy_refresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_controller.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_page_counter.dart';
import 'package:pornhub_app/http/api/api.dart';
import 'package:pornhub_app/model/arround/query_model.dart';
import 'package:pornhub_app/model/blogger_base_model.dart';
import 'package:pornhub_app/services/user_service.dart';

class BloggerListPageController extends BaseRefreshController
    with BaseRefreshPageCounter {
  final int loadType;
  final String city;
  final String province;
  BloggerListPageController(this.loadType, this.province, this.city);

  late QueryModel queryModel;

  int totalCount = 0;

  final userService = Get.find<UserService>();

  bool isLoadedData = false;
  @override
  void onInit() {
    queryModel =
        QueryModel(enableQuery: false, cityName: city, loadType: loadType);
    super.onInit();
  }

  final _pageSize = 48;
  List<BloggerBaseModel> bloggerItems = List<BloggerBaseModel>.of([]);

  @override
  Future<IndicatorResult> onRefresh() async {
    resetPage();

    List<BloggerBaseModel>? resp = !queryModel.enableQuery
        ? await Api.getNearBy(
            page: page,
            pageSize: _pageSize,
            cityName:
                queryModel.cityName == null || queryModel.cityName!.isEmpty
                    ? ""
                    : queryModel.cityName,
            loadType: loadType,
          )
        : await Api.getNearBy(
            page: page,
            pageSize: _pageSize,
            loadType: loadType,
            bodyShape: queryModel.bodyShape,
            emotion: queryModel.emotion,
            endAge: queryModel.endAge,
            endHeight: queryModel.endHeight,
            endWeight: queryModel.endWeight,
            intention: queryModel.intention,
            prefer: queryModel.prefer,
            startAge: queryModel.startAge,
            startHeight: queryModel.startHeight,
            startWeight: queryModel.startWeight,
            cityName:
                queryModel.cityName == null || queryModel.cityName!.isEmpty
                    ? ""
                    : queryModel.cityName);

    isLoadedData = true;
    if (resp == null) {
      return IndicatorResult.fail;
    }
    incPage();
    _setList(resp);
    return IndicatorResult.success;
  }

  @override
  Future<IndicatorResult> onLoad() async {
    List<BloggerBaseModel>? resp = !queryModel.enableQuery
        ? await Api.getNearBy(
            page: page,
            pageSize: _pageSize,
            cityName:
                queryModel.cityName == null || queryModel.cityName!.isEmpty
                    ? ""
                    : queryModel.cityName,
            loadType: 2,
          )
        : await Api.getNearBy(
            page: page,
            pageSize: _pageSize,
            loadType: 2,
            bodyShape: queryModel.bodyShape,
            emotion: queryModel.emotion,
            endAge: queryModel.endAge,
            endHeight: queryModel.endHeight,
            endWeight: queryModel.endWeight,
            intention: queryModel.intention,
            prefer: queryModel.prefer,
            startAge: queryModel.startAge,
            startHeight: queryModel.startHeight,
            startWeight: queryModel.startWeight,
            cityName:
                queryModel.cityName == null || queryModel.cityName!.isEmpty
                    ? ""
                    : queryModel.cityName);
    isLoadedData = true;
    if (resp == null) {
      return IndicatorResult.fail;
    }

    _addList(resp);
    if (resp.length < _pageSize) {
      return IndicatorResult.noMore;
    }
    incPage();
    return IndicatorResult.success;
  }

  void _setList(List<BloggerBaseModel>? list) {
    bloggerItems.clear();
    _addList(list);
  }

  void _addList(List<BloggerBaseModel>? list) {
    if (null != list && list.isNotEmpty) {
      bloggerItems.addAll(list);
    }
    if (bloggerItems.isEmpty) {
      totalCount = 0;
    } else {
      totalCount = bloggerItems.length % 12 > 0
          ? bloggerItems.length ~/ 12 + 1
          : bloggerItems.length ~/ 12;
    }
    update();
  }

  initQuery(QueryModel q) async {
    queryModel = q;
    await onRefresh();
    update();
  }
}
